﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Interface.weixin.Html
{
    public partial class WxBindMemberHtml : UrlRewritePage
    {
        public string owner;
        public string openId;
        public WxMemberCard _memberCard;
        //public WxMemberCardConfig _Config;
        protected void Page_Load(object sender, EventArgs e)
        {
            owner = Request["account"];
            openId = Request["OpenId"];
        }

        #region 绑定会员卡号 + protected void btnBind_Click(object sender, EventArgs e)
        /// <summary>
        /// 绑定会员卡号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBind_Click(object sender, EventArgs e)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _memberCard = session.CreateCriteria<WxMemberCard>().Add(Expression.Eq("OwnerAccount", owner)).Add(Expression.Eq("WxId", openId)).UniqueResult<WxMemberCard>();
                    if (_memberCard == null)
                    {
                        _memberCard = session.CreateCriteria<WxMemberCard>().Add(Expression.Eq("OwnerAccount", owner)).Add(Expression.Eq("CardNo", txtMemberCardNo.Text.Trim())).UniqueResult<WxMemberCard>();

                        if (_memberCard != null)
                        {
                            //绑定微信号
                            _memberCard.MemberAccount = owner;

                            session.Save(_memberCard);
                            session.Flush();

                            Response.Write("绑定成功!你的会员卡号是:" + _memberCard.CardNo);
                        }
                        else
                        {
                            Response.Write("绑定失败!");
                        }
                    }
                    else
                    {
                        Response.Write("您已经拥有会员卡了.");
                    }
                   
                }
                catch(Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        } 
        #endregion
    }
}